package cn.chenhuanming.shh.administer.intermediary.business.eventHandler;

import cn.chenhuanming.shh.administer.entity.jpa.House;
import cn.chenhuanming.shh.administer.entity.jpa.HouseQualityApply;
import cn.chenhuanming.shh.administer.entity.jpa.UserBroker;
import cn.chenhuanming.shh.administer.entity.jpa.UserSystem;
import cn.chenhuanming.shh.administer.exception.NotOneSelfException;
import cn.chenhuanming.shh.administer.exception.ResourceExistedException;
import cn.chenhuanming.shh.administer.repository.HouseQualityApplyRepository;
import cn.chenhuanming.shh.administer.repository.HouseRepository;
import cn.chenhuanming.shh.administer.repository.UserBrokerRepository;
import cn.chenhuanming.shh.administer.repository.UserSystemRepository;
import cn.chenhuanming.shh.administer.utils.AuthorityTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
import org.springframework.data.rest.core.annotation.HandleBeforeDelete;
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Component
@RepositoryEventHandler
@Transactional
public class HouseQualityApplyEventHandle {

    @Autowired
    HouseQualityApplyRepository houseQualityApplyRepository;

    @Autowired
    UserBrokerRepository userBrokerRepository;

    @Autowired
    UserSystemRepository userSystemRepository;

    @Autowired
    HouseRepository houseRepository;

    @PreAuthorize("hasRole('SHOP_BROKER')")
    @HandleBeforeCreate
    public void beforeCreate(HouseQualityApply houseQualityApply) {
        UserBroker broker = userBrokerRepository.getOne(AuthorityTool.getPrincipal());

        //非本经纪人操作
        if (!broker.getUsername().equals(houseQualityApply.getHouse().getUserBroker().getUsername()))
            throw new NotOneSelfException();

        //房源已经申请
        if (!houseQualityApply.getHouse().getIsQuality().equals("否"))
            throw new ResourceExistedException();

        houseQualityApply.getHouse().setIsQuality("审批中");
        houseRepository.save(houseQualityApply.getHouse());

        houseQualityApply.setUserBrokerByApplicant(broker);
        houseQualityApply.setApplyName(broker.getName());
        houseQualityApply.setApplyTime(LocalDateTime.now());

        UserSystem system = broker.getUserSystem();
        houseQualityApply.setUserBrokerByExaminer(system);
        houseQualityApply.setExamineName(system.getName());
    }

    @PreAuthorize("hasRole('ROLE_SHOP_MANAGER')")
    @HandleBeforeSave
    public void beforeSave(HouseQualityApply houseQualityApply) {
        UserSystem system = userSystemRepository.getOne(AuthorityTool.getPrincipal());

        if (!system.getUsername().equals(houseQualityApply.getUserBrokerByExaminer().getUsername()))
            throw new NotOneSelfException();

        houseQualityApply.setExamintTime(LocalDateTime.now());
        HouseQualityApply hqa = houseQualityApplyRepository.getOne(houseQualityApply.getId());
        House house = hqa.getHouse();
        if (houseQualityApply.getExamineStatus().equals("审批通过")) {
            house.setIsQuality("是");
            houseRepository.save(house);
        }
        if (houseQualityApply.getExamineStatus().equals("审批不通过")) {
            house.setIsQuality("否");
            houseRepository.save(house);
        }
    }

    @PreAuthorize("hasAnyRole('ROLE_SHOP_MANAGER', 'SHOP_BROKER', 'ROLE_ADMINISTER')")
    @HandleBeforeDelete
    public void beforeDelete(Integer id) {
        House house = houseQualityApplyRepository.getOne(id).getHouse();
        house.setIsQuality("否");
        houseRepository.save(house);
    }
}
